/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ugd.controller;

import edu.ugd.domain.EncodingForm;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Ivan.Arsov
 */
public class Base {

    @ModelAttribute
    public EncodingForm getLoginForm(HttpSession session) throws InstantiationException, IllegalAccessException {
        return (EncodingForm) getSessionForm(session, EncodingForm.class);
    }

    protected Object getSessionForm(HttpSession session, Class<?> formClass) {
        String attrName = formClass.getSimpleName().substring(0, 1).toLowerCase() + formClass.getSimpleName().substring(1);
        Object form = session.getAttribute(attrName);
        if (form == null) {
            try {
                form = formClass.newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            session.setAttribute(attrName, form);
        }
        return form;
    }

    protected Map<String, String> getEncodings() {
        Map<String, String> encodings = new LinkedHashMap<String, String>();
        encodings.put("UTF-8", "UTF-8");      
        encodings.put ("ASCII", "ASCII");
        encodings.put ("ISO-8859-1", "ISO-8859-1");
        encodings.put ("ISO-8859-2", "ISO-8859-2");
        encodings.put ("ISO-8859-3", "ISO-8859-3");
        encodings.put ("ISO-8859-4", "ISO-8859-4");
        encodings.put ("ISO-8859-5", "ISO-8859-5");
        encodings.put ("ISO-8859-6", "ISO-8859-6");
        encodings.put ("ISO-8859-7", "ISO-8859-7");
        encodings.put ("ISO-8859-8", "ISO-8859-8");
        encodings.put ("ISO-8859-9", "ISO-8859-9");
        encodings.put("Windows-1250", "Windows-1250");
        encodings.put("Windows-1251", "Windows-1251");
        encodings.put("Windows-1252", "Windows-1252");
        encodings.put("Windows-1253", "Windows-1253");
        encodings.put("Windows-1254", "Windows-1254");
        encodings.put ("UTF-16", "UTF-16");
        encodings.put ("UTF-16BE", "UTF-16BE");
        encodings.put ("UTF-16LE", "UTF-16LE");
        encodings.put ("UTF-32", "UTF-32");
        encodings.put ("UTF-32BE", "UTF-32BE");
        encodings.put ("UTF-32LE", "UTF-32LE");

        
        
        
        
        
        
        return encodings;
    }
}
